package ch10_thread.thread;

/**
 * 带有sleep的中断线程停止的写法
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class StopSleep {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            int num = 0;
            while (num < Integer.MAX_VALUE / 2 && !Thread.currentThread().isInterrupted()) {
                if (num % 10000 == 0) {
                    System.out.println(num + "是10000的倍数");
                }
                num++;
            }
            try {
                // 阻塞中被打断
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("执行结束...");
        });
        thread.start();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();
    }
}
